//
//  ANProfileViewController.m
//  Ants
//
//  Created by ZHM on 16/3/15.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANProfileViewController.h"

#import "ANUserInfoTableViewController.h"
#import "ANUserLoginViewController.h"
#import "ANProfieleHeader.h"
#import "ANSellerTableViewController.h"
#import "ANManageTableViewController.h"
#import "ANNavigationVC.h"

#define ANLoginOKNote @"loginOKNote"
#define ANlogoutComplete @"logoutComplete"

@interface ANProfileViewController ()

@property (nonatomic, strong) ANBaseTableViewController *tableViewViewController;
@property (nonatomic, copy) NSString *loginUserName;
@property (nonatomic, weak) ANArrowItem *user;
@property (nonatomic, weak) ANArrowItem *seller;
@property (nonatomic, weak) ANArrowItem *manage;

@end

@implementation ANProfileViewController

- (NSString *)loginUserName {
    if (!_loginUserName) {
        if ([sharedWebService isLoggedIn]) {
            _loginUserName = [sharedWebService currentUser].username;
        }else {
            _loginUserName = @"未登录";
        }
    }
    return _loginUserName;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置导航条
    [self setUpNav];
    
    //添加第1组
    [self setUpGroup_1];
    //添加第2组
    [self setUpGroup_2];
    //添加第3组
    [self setUpGroup_3];
    //添加第3组
    [self setUpGroup_4];
    
    //返回界面需要更新数据-这里设置监听
    //监听登录成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginOK) name:ANLoginOKNote object:nil];
    //账号注销
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:ANlogoutComplete object:nil];
}
//账号注销
- (void)logout {
    _loginUserName = @"未登录";
    _user.subTilte = _loginUserName;
    _user.destVcClass = nil;
    __weak typeof(self) weakSelf = self;
    _user.option = ^(ANProfileBaseItem *item){
        [weakSelf showLoginVC];
    };
    _seller.destVcClass = nil;
    _seller.option = ^(ANProfileBaseItem *item){
        [weakSelf showLoginVC];
    };
    _manage.destVcClass = nil;
    _manage.option = ^(ANProfileBaseItem *item){
        [weakSelf showLoginVC];
    };
    [self.tableView reloadData];
}
- (void)showLoginVC {
    ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
    ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
    [self presentViewController:nav animated:YES completion:nil];
}
- (void)loginOK {
    _loginUserName = [sharedWebService currentUser].username;
    if (!_loginUserName) {
        _loginUserName = [sharedWebService currentUser].mobilePhoneNumber;
    }
    _user.subTilte = _loginUserName;
    _user.destVcClass = [ANUserInfoTableViewController class];
    _user.option = nil;
    _seller.destVcClass = [ANSellerTableViewController class];
    _seller.option = nil;
    _manage.destVcClass = [ANManageTableViewController class];
    _manage.option = nil;
    [self.tableView reloadData];
}
- (void)setUpGroup_1 {
    ANArrowItem *user = [ANArrowItem itemWithTilte:@"用户登录" subTilte:self.loginUserName image:[UIImage imageNamed:@"user"]];
    if ([sharedWebService isLoggedIn]) {
        user.destVcClass = [ANUserInfoTableViewController class];
    } else {
        user.option = ^(ANProfileBaseItem *item){
            ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
            ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
            [self presentViewController:nav animated:YES completion:nil];
        };
    }
    _user = user;
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    
    group.items = @[user];
    [self.guoups addObject:group];
}

- (void)setUpGroup_2 {
    ANArrowItem *wallet = [ANArrowItem itemWithTilte:@"我的钱包" subTilte:@"积分/代金券" image:[UIImage imageNamed:@"wallet"]];
    //ANArrowItem *seting = [ANArrowItem itemWithTilte:@"设置"  image:[UIImage imageNamed:@"seting"]];
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    
    group.items = @[wallet];
    [self.guoups addObject:group];
}

- (void)setUpGroup_3 {
    ANArrowItem *seller = [ANArrowItem itemWithTilte:@"我是卖家" subTilte:@"发布/管理商品" image:[UIImage imageNamed:@"wallet"]];
    if ([sharedWebService isLoggedIn]) {
        seller.destVcClass = [ANSellerTableViewController class];
    } else {
        seller.option = ^(ANProfileBaseItem *item){
            ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
            ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
            [self presentViewController:nav animated:YES completion:nil];
        };
    }
    _seller = seller;
    ANArrowItem *seting = [ANArrowItem itemWithTilte:@"已卖出"  image:[UIImage imageNamed:@"draft"]];
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    
    group.items = @[seller,seting];
    [self.guoups addObject:group];
}

- (void)setUpGroup_4 {
    ANArrowItem *manage = [ANArrowItem itemWithTilte:@"管理员" subTilte:@"管理商品" image:[UIImage imageNamed:@"wallet"]];
    if ([sharedWebService isLoggedIn]) {
        manage.destVcClass = [ANManageTableViewController class];
    } else {
        manage.option = ^(ANProfileBaseItem *item){
            ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
            ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
            [self presentViewController:nav animated:YES completion:nil];
        };
    }
    _manage = manage;
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    group.items = @[manage];
    [self.guoups addObject:group];
}

- (void)setUpNav {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemCilck)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

#pragma mark -- "设置" 按钮事件
- (void)rightItemCilck{
//    HMSettingViewController *vc = [[HMSettingViewController alloc] init];
//    //隐藏底部工具条
//    vc.hidesBottomBarWhenPushed = YES;
//    
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 返回每行CELL高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 0) {
        return 60;
    }
    return 44;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
